var TongYin = GownFullModule.extend({
	ime: null,
	current_list: null,
	current_query: null,
	target_list: 'Num',
	type_table: null,
	constructor: function()
	{
		var y = this;

		this._canton_tongyin_fun = function(x) {
			if(x instanceof Function) {
				var dxchg = x();

				if(dxchg && !dxchg.htmlp) y.Generate('canton',dxchg.value);
				return x;
			}
			else return x;
		}
		this._mandarin_tongyin_fun = function(x) {
			if(x instanceof Function) {
				var dxchg = x();

				if(dxchg && !dxchg.htmlp) y.Generate('mandarin',dxchg.value);
				return x;
			}
			else return x;
		}
		this._keyboard_observer = function(obj,type,key,modifiers,b)
		{
			if(!b) return false;
			if(type == Const.KEY_TYPE_PRESS) y.StopLoading();
			return true;
		};
		this.type_table = {};
		this.type_table['canton'] = $GFS('Cantonese');
		this.type_table['mandarin'] = $GFS('Mandarin');
	},
	module_name: function()
	{
		return 'TongYin';
	},
	init_module: function(ime,modman)
	{
		var b;

		this.ime = ime; // store reference to IME for further use.
		
		b = GFFunctionalInterpreter.RegisterFunctional('canton_tongyin',this._canton_tongyin_fun);
		if(!b) {
			Debug.present && Debug.trace(0,'TongYin: Error, functional :canton_tongyin is already registered.');
			return false;
		}
		b = GFFunctionalInterpreter.RegisterFunctional('mandarin_tongyin',this._mandarin_tongyin_fun);
		if(!b) {
			Debug.present && Debug.trace(0,'TongYin: Error, functional :mandarin_tongyin is already registered.');
			return false;
		}
		this.ime.AddKeyboardObserver(this._keyboard_observer);
		return true;
	},
	cleanup_module: function()
	{
		this.ime.RemoveKeyboardObserver(this._keyboard_observer);
		GFFunctionalInterpreter.UnregisterFunctional('canton_tongyin');
		GFFunctionalInterpreter.UnregisterFunctional('mandarin_tongyin');
		// and cancel all loading...
		this.StopLoading();
		return true;
	},
	Generate: function(type,str)
	{
		this.StopLoading();

		this.current_query = str;
		this.current_type = type;
		url = 'http://127.0.0.1/backend/tongyin/tongyin.php?type=' + type + '&code=' + encodeURI(str);
		DownloadManager.NewDownloadName('TongYin_Callback',url,function(userdata,serverdata) { userdata.Callback(serverdata); },this,true);
	},
	Callback: function(ret)
	{
		var x = this;
		var candlist;
		var i;
		var title;

		if(!ret || ret.length <= 0) {
			this.ime.candwin.ReleaseList(this,this.target_list);
			this.ime.candwin.UpdateCandidate();
		}
		else {
			this.current_list = new Array();
			for(i=1;i<ret.length;i++) this.current_list.push(lib.utf16char(ret[i]));

			Debug.present && Debug.trace(1,'TongYin: data=(%s)',this.current_list);
			candlist = this.ime.candwin.OccupyList(this,this.target_list);
			if(candlist) {
				title = $GFSv('Similar $1 phonetic of "$2"',this.type_table[this.current_type],this.current_query);
				candlist.SetList(title,this.current_list,function(obj,pos) { return x.CandidateHandler(obj,pos); });
				this.ime.candwin.UpdateCandidate();
			}
			else {
				Debug.present && Debug.trace(1,'TongYin: %s is occupied.',this.target_list);
			}
		}
	},
	CandidateHandler: function(obj,pos)
	{
		var str;

		str = this.current_list[pos];
		str = str.replace(new RegExp('^' + this.current_query),'');
		this.ime._SendString(obj,str,true);
		return true;
	},
	StopLoading: function()
	{
		DownloadManager.CancelDownloadName('TongYin_Callback');
		this.ime.candwin.ReleaseList(this,this.target_list);
		this.ime.candwin.UpdateCandidate();
	}
});

